#include "task_list.hpp"
#include "lock_guard.hpp"
#include <iostream>

using std::cout;
using std::endl;
namespace rocket
{
task_list::task_list(int size)
:_capacity(size)
,_lock()
,_waiting_to_push(_lock.get_mutex_lock_pointer())
,_waiting_to_pop(_lock.get_mutex_lock_pointer())
,_no_need_to_wait(false)
{
    
}

void task_list::push(elemtype2 ele)
{
    lock_guard guard(_lock.get_mutex_lock_pointer());
    while(full()==true)
    {
        _waiting_to_push.wait();
    }
    _to_do_list.push(ele);
    cout<<"task_list::push()"<<endl;
    _waiting_to_pop.notify_one();

}
elemtype2 task_list::pop()
{
    lock_guard guard(_lock.get_mutex_lock_pointer());

    while(empty()==true)
    {
        if(empty()==true&&_no_need_to_wait==true)
        {
            return nullptr;
        }
        _waiting_to_pop.wait();  

    }
    elemtype2 temp=_to_do_list.front();
    _to_do_list.pop();
    cout<<"task_list::pop()"<<endl;

    _waiting_to_push.notify_one();

    return temp;
}

bool task_list::empty()
{
    return _to_do_list.empty();
}

bool task_list::full()
{
    return _to_do_list.size()==_capacity;
}

void task_list::notify_all()
{
    _no_need_to_wait=true;
    _waiting_to_pop.notify_all();
}

}

